﻿using System;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using ERP.Page;
using ET;
using BLL;

namespace ERP.PageUC
{
    public partial class Customer_TypeUC : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnSavePopup_Click(object sender, EventArgs e)
        {
            if (lbID.Value == "")
            {
                InsertCustomer_Type();
            }
            else
            {
                UpdateCustomer_Type();
            }
        }

        protected void btnReloadPopup_Click(object sender, EventArgs e)
        {

        }
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            var clsCustomer_Type = new Customer_Type_BLL();
            var dtCustomer_Type = clsCustomer_Type.BindGirdtb_Customer_Type(string.Empty);
            if (dtCustomer_Type != null && dtCustomer_Type.Rows.Count > 0)
            {
                foreach (DataRow dr in dtCustomer_Type.Rows)
                {
                    if (txtMaLoaiKhachHang.Text == dr["Customer_Type_Alias"].ToString())
                    {
                        kiemtra = true;
                        if (Page.Master != null)
                            ((Label)Page.Master.FindControl("lbThongBao")).Text = "Trùng mã loại khách hàng !";
                        OpenDialog("#thongbao");
                        if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    }
                    else if (txtLoaiKhachHang.Text == dr["Customer_Type_Name"].ToString())
                    {
                        kiemtra = true;
                        if (Page.Master != null)
                            ((Label)Page.Master.FindControl("lbThongBao")).Text = "Trùng loại khách hàng !";
                        OpenDialog("#thongbao");
                        if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertCustomer_Type()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new Customer_Type_ET
                    {
                        Customer_Type_Alias = txtMaLoaiKhachHang.Text,
                        Customer_Type_Name = txtLoaiKhachHang.Text,
                        Customer_Type_Date_Add = DateTime.Now,
                        Customer_Type_Date_Modify = DateTime.Now
                    };
                    Customer_Type_BLL.Inserttb_Customer_Type(obj);
                    ClearTextBox();
                    if (Page.Master != null)
                        ((Label)Page.Master.FindControl("lbThongBao")).Text = "Thêm mới thành công !";
                }
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                txtMaLoaiKhachHang.Enabled = true;
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }
        protected void UpdateCustomer_Type()
        {
            try
            {
                var obj = new Customer_Type_ET
                {
                    Customer_Type_ID = int.Parse(lbID.Value),
                    Customer_Type_Alias = txtMaLoaiKhachHang.Text,
                    Customer_Type_Name = txtLoaiKhachHang.Text,
                    Customer_Type_Date_Add = DateTime.Now,
                    Customer_Type_Date_Modify = DateTime.Now
                };
                Customer_Type_BLL.Updatetb_Customer_Type(obj);
                ((Customer_Type)Page).GetCustomer_Type();
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Sữa thành công !";
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                txtMaLoaiKhachHang.Enabled = false;
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            var sb = new StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                "scriptKey", sb.ToString(), true);
        }
        #endregion
        #region[ClearTextbox]
        protected void ClearTextBox()
        {
            txtMaLoaiKhachHang.Text = string.Empty;
            txtLoaiKhachHang.Text = string.Empty;
        }
        #endregion
    }
}